***************************************************
* This do-file creates supplementary tables
* not included in the published paper or appendix
***************************************************

clear all
set more off
set maxvar 10000
set matsize 5000
cap version 15 
set seed 123456789

* SET DIRECTORY HERE AS MAIN FOLDER 
global directory "[SET DIRECTORY HERE]"

* Subfolder globals
* -----------------

	global data				"${directory}/01_data"
	global tables			"${directory}/02_output/tables"
	sysdir set PLUS 		"${directory}/ado"

do "${tables}/supplementary/stata-tex.do" 

*** Supplementary tables (referees only)
* ----------------------------------------

* TABLES S1-S3
* ----------------


		** Women sample
		
		use "${data}/data_women.dta" , clear
		local i = 1
		gen pvalue_t0 = .
		gen pvalue_t1 = .
		gen pvalue_t2 = .
		gen pvalue_t3 = .
		gen varname = ""

		
		foreach var in 	iga_precovid c1_headjob c1_othermembersworkn c1_headincome30 c1_incomeotheriga c1_hoursprimjob c1_hengag_agr chemicals ///
						value19_n_iga z_financial_index  z_agency_index c1_cropprodna c1_cropvalna ///
						d1_ladder_present d1_ladder_1ago d1_ladder3years d1_ladderwealth mhi5 ///
						z_assets19_total c1_job_iga business_profit business_employee total_employement ///
						value_n_iga does_livestock conso_tot_pc conso_food_pc conso_nofood_pc ///
						z_assets_total b4_econshock b4_othershock b4_extreme b4_normal migration_d hh_migrated migration_future_d {
				
				noi di "`var'"
				replace varname = "`var'" if _n == `i'
				sum `var' if trt_cash == 0
				local sd = `r(sd)'
				insert_into_file using "sample_table.csv", key(`var'_nocontrols_cmean) value(" `r(mean)'") format(%6.3f)
				insert_into_file using "sample_table.csv", key(`var'_nocontrols_csd) value(" `sd'") format(%6.3f)
				
				** Equation 1
				reg `var' trt_cash , ro
				mat A = r(table)
				local p = A[4,1]
				replace pvalue_t0 = `p' if _n == `i'
				store_est_tpl using "sample_table.csv" , coef(trt_cash) name(`var'_nocontrols_t) all
			
				** Equation 2
				reg `var' trt_cash_0 trt_cash_1 ,  ro
				mat A = r(table)
				local p0 = A[4,1]
				replace pvalue_t1 = `p0' if _n == `i'
				local p1 = A[4,2]
				replace pvalue_t2 = `p1' if _n == `i'
				store_est_tpl using "sample_table.csv" , coef(trt_cash_0) name(`var'_nocontrols_t0) all
				store_est_tpl using "sample_table.csv" , coef(trt_cash_1) name(`var'_nocontrols_t1) all				
				local n = `e(N)'
				insert_into_file using "sample_table.csv", key(`var'_t1_nocontrols_n) value(" `n'") format(%-9.0fc)
				test trt_cash_0 = trt_cash_1
				replace pvalue_t3 = `r(p)' if _n == `i'
				local ++i
				insert_into_file using "sample_table.csv", key(`var'_test_nocontrols) value(" `r(p)'") format(%6.3f)
		
		}

		
			
		** Partner sample	
		
		preserve
		use "${data}/data_partners.dta" , clear
		
		foreach var in 	z_male_index {
				sum `var' if trt_cash_part_1==0
				local sd = `r(sd)'
				insert_into_file using "sample_table.csv", key(`var'_nocontrols_cmean) value(" `r(mean)'") format(%6.3f)
				insert_into_file using "sample_table.csv", key(`var'_nocontrols_csd) value(" `sd'") format(%6.3f)
				reg `var' trt_cash_part_1 , ro
				mat A = r(table)
				local p_dialogue = A[4,1]
				di `p_dialogue'
				store_est_tpl using "sample_table.csv" , coef(trt_cash_part_1) name(`var'_t1_nocontrols) all
		}
		restore

		
		
		** FDR q-values
			
			drop if pvalue_t0==. 
			keep pvalue* varname	
			reshape long pvalue_t , i(varname) j(t)
			set obs `=_N+1'
			replace varname = "z_male_index" if _n == 145
			replace pvalue_t = `p_dialogue' if _n == 145
			replace t = 2 if _n == 145
			rename pvalue_t pval
			do "${data}/fdr_sharpened_qvalues.do"
			foreach var in c1_job_iga iga_precovid business_profit business_employee total_employement c1_hoursprimjob c1_headjob c1_othermembersworkn c1_headincome30 c1_incomeotheriga /// 
						c1_hengag_agr chemicals c1_cropprodna c1_cropvalna does_livestock value19_n_iga value_n_iga /// 
						z_financial_index z_agency_index d1_ladder_present d1_ladder_1ago d1_ladder3years d1_ladderwealth mhi5 /// 
						conso_tot_pc conso_food_pc conso_nofood_pc z_assets_total z_assets19_total hh_migrated migration_d migration_future_d b4_econshock b4_othershock b4_extreme b4_normal z_male_index {
			sum bky06_qval if varname == "`var'" & t == 0
			insert_into_file using sample_table.csv, key(`var'_qpvalue_t_nocontrols) value(" `r(mean)'") format(%6.3f)
			sum bky06_qval if varname == "`var'" & t == 1
			insert_into_file using sample_table.csv, key(`var'_qpvalue_t0_nocontrols) value(" `r(mean)'") format(%6.3f)
			sum bky06_qval if varname == "`var'" & t == 2
			insert_into_file using sample_table.csv, key(`var'_qpvalue_t1_nocontrols) value(" `r(mean)'") format(%6.3f)
			sum bky06_qval if varname == "`var'" & t == 3
			insert_into_file using sample_table.csv, key(`var'_qpvalue_test_nocontrols) value(" `r(mean)'") format(%6.3f) 
			}

		
		table_from_tpl, t("TPL_tabS1.tex") r("sample_table.csv") o("tabS1.tex")	
		table_from_tpl, t("TPL_tabS2.tex") r("sample_table.csv") o("tabS2.tex")	
		table_from_tpl, t("TPL_tabS3.tex") r("sample_table.csv") o("tabS3.tex")	
	
		


* TABLE S4
* ----------

		use "${data}/data_women.dta" , clear
		drop if attrition == 1
		do "${data}/Lasso_var.do"
		

		
			foreach var in 	c1_activsearchwork c1_activsearchwork_6m c1_activsearchwork_12m ///
							z_social z_civic i2_taxesintro z_antisoc z_prosoc z_victimization z_conflict  {
			sum `var' if trt_cash==0
			local sd = `r(sd)'
			insert_into_file using "sample_table.csv", key(`var'_cmean) value(" `r(mean)'") format(%6.3f)
			insert_into_file using "sample_table.csv", key(`var'_csd) value(" `sd'") format(%6.3f)
			pdslasso `var' trt_cash ($balance $X_poly_and_int i.a1_enumerator tclp) , partial(i.a1_enumerator tclp) robust
			local n = `e(N)'
			insert_into_file using "sample_table.csv", key(`var'_t1_n) value(" `n'") format(%-9.0fc)
			store_est_tpl using "sample_table.csv" , coef(trt_cash) name(`var'_t) all
			pdslasso `var' trt_cash_0 trt_cash_1 ($balance $X_poly_and_int i.a1_enumerator tclp) , partial(i.a1_enumerator tclp) robust
			store_est_tpl using "sample_table.csv" , coef(trt_cash_0) name(`var'_t0) all
			store_est_tpl using "sample_table.csv" , coef(trt_cash_1) name(`var'_t1) all
			test trt_cash_0 = trt_cash_1
			insert_into_file using "sample_table.csv", key(`var'_test) value(" `r(p)'") format(%6.3f)
			}
	
	
		table_from_tpl, t("TPL_tabS4.tex") r("sample_table.csv") o("tabS4.tex")




* TABLES S5-S7
* ----------------

		use "${data}/data_women.dta" , clear
		drop if attrition==1
		do "${data}/Lasso_var2.do"

		replace tclp = . if tclp_missing == 1
		
		global balance		woman_b repondant_age_b secondary_b  origine_naissance_b relation_head_b relation_spouse_b relation_daughter_b relation_other_b initiatives_2_b formation_b ///
			   				hhsize_b children_b adults_b elderly_b dirtfloor_b thatched_still_b proprietaire_terre_b livestock_b /// 
							distance_cheflieu_b distance_marche_b distance_transpublic_b distance_ecoleprim_b distance_eau_b 
							
		*** IGA, married, conso					
			foreach x in c1_job_iga_bl married_b conso2_bl {
					gen interaction = trt_cash*`x'
					foreach var in c1_job_iga iga_precovid business_profit business_employee total_employement c1_headjob c1_othermembersworkn c1_headincome30 c1_incomeotheriga /// 
						c1_hengag_agr chemicals c1_cropprodna c1_cropvalna does_livestock value19_n_iga value_n_iga /// 
						z_financial_index z_agency_index d1_ladder_present d1_ladder_1ago d1_ladder3years d1_ladderwealth mhi5 /// 
						conso_tot_pc conso_food_pc conso_nofood_pc z_assets_total z_assets19_total migration_d migration_future_d b4_econshock b4_othershock b4_extreme b4_normal  {
					pdslasso `var' trt_cash `x' interaction ($balance $X_poly_and_int i.a1_enumerator tclp) , partial(i.a1_enumerator tclp) robust
					store_est_tpl using "sample_table.csv" , coef(trt_cash) name(hte1_`var'_`x') all
					local n = `e(N)'
					insert_into_file using "sample_table.csv", key(hte1_`var'_`x'_n) value(" `n'") format(%-9.0fc)
					store_est_tpl using "sample_table.csv" , coef(interaction) name(hte3_`var'_`x') all
					local n = `e(N)'
					insert_into_file using "sample_table.csv", key(hte3_`var'_`x'_n) value(" `n'") format(%-9.0fc)
					lincom trt_cash + interaction
					local coef = `r(estimate)'
					local se = `r(se)'
					local text : di %6.3f `coef'
					local pvalue = `r(p)'
					count_stars, p(`pvalue')
					insert_into_file using sample_table.csv, key(hte_`var'_`x'_starbeta) value(" `text'`r(stars)'") format(%6.3f)
					insert_into_file using sample_table.csv, key(hte_`var'_`x'_se) value(" `se'") format(%6.3f)
					}
					drop interaction
					}
		
		*** Participation in TCLP
			foreach x in tclp {
					gen interaction = trt_cash*`x'
					foreach var in c1_job_iga iga_precovid business_profit business_employee total_employement c1_headjob c1_othermembersworkn c1_headincome30 c1_incomeotheriga /// 
						c1_hengag_agr chemicals c1_cropprodna c1_cropvalna does_livestock value19_n_iga value_n_iga /// 
						z_financial_index z_agency_index d1_ladder_present d1_ladder_1ago d1_ladder3years d1_ladderwealth mhi5 /// 
						conso_tot_pc conso_food_pc conso_nofood_pc z_assets_total z_assets19_total migration_d migration_future_d b4_econshock b4_othershock b4_extreme b4_normal  {
					pdslasso `var' trt_cash `x' interaction ($balance $X_poly_and_int i.a1_enumerator) , partial(i.a1_enumerator) robust
					store_est_tpl using "sample_table.csv" , coef(trt_cash) name(hte1_`var'_`x') all
					local n = `e(N)'
					insert_into_file using "sample_table.csv", key(hte1_`var'_`x'_n) value(" `n'") format(%-9.0fc)
					store_est_tpl using "sample_table.csv" , coef(interaction) name(hte3_`var'_`x') all
					local n = `e(N)'
					insert_into_file using "sample_table.csv", key(hte3_`var'_`x'_n) value(" `n'") format(%-9.0fc)
					lincom trt_cash + interaction
					local coef = `r(estimate)'
					local se = `r(se)'
					local text : di %6.3f `coef'
					local pvalue = `r(p)'
					count_stars, p(`pvalue')
					insert_into_file using sample_table.csv, key(hte_`var'_`x'_starbeta) value(" `text'`r(stars)'") format(%6.3f)
					insert_into_file using sample_table.csv, key(hte_`var'_`x'_se) value(" `se'") format(%6.3f)
					}
					drop interaction
					}
		
		
		table_from_tpl, t("TPL_tabS5.tex") r("sample_table.csv") o("tabS5.tex")
		table_from_tpl, t("TPL_tabS6.tex") r("sample_table.csv") o("tabS6.tex")
		table_from_tpl, t("TPL_tabS7.tex") r("sample_table.csv") o("tabS7.tex")
	
		
		
							********************************************************	
							****** Robustness checks to adding other HTE*************
							********************************************************
							
							cap drop interaction
							
							global balance_hte 
							foreach var in $balance {
							cap drop hte_`var'
							gen hte_`var'  = trt_cash*`var'
							global balance_hte $balance_hte hte_`var'
							}
							
							
							tab a1_enumerator , gen(a1_enumerator)
							global enumerator_list a1_enumerator2 a1_enumerator3 a1_enumerator4 a1_enumerator5 a1_enumerator6 a1_enumerator7 a1_enumerator8 a1_enumerator9 a1_enumerator10 a1_enumerator11 a1_enumerator12 a1_enumerator13 a1_enumerator14 a1_enumerator15 a1_enumerator16 a1_enumerator17 a1_enumerator18 a1_enumerator19 a1_enumerator20 a1_enumerator21 a1_enumerator22 a1_enumerator23 a1_enumerator24
							
							
							************************************
							* job at baseline (c1_job_iga_bl) 
							***************************************
							
							* does_livestock: robust
							cap drop interaction 
							gen interaction = trt_cash*c1_job_iga_bl
							pdslasso does_livestock trt_cash c1_job_iga_bl interaction  ($balance $balance_hte $X_poly_and_int $enumerator_list ) , partial($balance $balance_hte $enumerator_list) robust
							
							
							* b4_othershock: robust
							cap drop interaction 
							gen interaction = trt_cash*c1_job_iga_bl
							pdslasso b4_othershock trt_cash c1_job_iga_bl interaction  ($balance $balance_hte $X_poly_and_int $enumerator_list ) , partial($balance $balance_hte $enumerator_list) robust
							
							
							************************************
							* married at baseline (married_b) 
							***************************************
							
							* Other HH members income (in Dinars): c1_incomeotheriga  -- > NOT ROBUST
							cap drop interaction 
							gen interaction = trt_cash*married_b
							pdslasso value19_n_iga trt_cash c1_incomeotheriga interaction  ($balance $balance_hte $X_poly_and_int $enumerator_list ) , partial($balance $balance_hte $enumerator_list) robust
							
							
							* Number of other HH members with an IGA: c1_othermembersworkn -- > NOT ROBUST
							cap drop interaction 
							gen interaction = trt_cash*married_b
							pdslasso value19_n_iga trt_cash c1_othermembersworkn interaction  ($balance $balance_hte $X_poly_and_int $enumerator_list ) , partial($balance $balance_hte $enumerator_list) robust
							
							
							* Livestock Bought since January 2019 (in Dirhams): value19_n_iga -- > NOT ROBUST
							cap drop interaction 
							gen interaction = trt_cash*married_b
							pdslasso value19_n_iga trt_cash married_b interaction  ($balance $balance_hte $X_poly_and_int $enumerator_list ) , partial($balance $balance_hte $enumerator_list) robust
							
							
							* Women empowerment: z_agency_index -- > NOT ROBUST
							cap drop interaction 
							gen interaction = trt_cash*married_b
							pdslasso z_agency_index trt_cash married_b interaction  ($balance $balance_hte $X_poly_and_int $enumerator_list ) , partial($balance $balance_hte $enumerator_list) robust
							
							
							* Women Financial Index: z_financial_index -- > NOT ROBUST
							cap drop interaction 
							gen interaction = trt_cash*married_b
							pdslasso z_financial_index trt_cash married_b interaction  ($balance $balance_hte $X_poly_and_int $enumerator_list ) , partial($balance $balance_hte $enumerator_list) robust
							
							
							* Relative wealth: d1_ladderwealth -- > NOT ROBUST
							cap drop interaction 
							gen interaction = trt_cash*married_b
							pdslasso d1_ladderwealth trt_cash married_b interaction  ($balance $balance_hte $X_poly_and_int $enumerator_list ) , partial($balance $balance_hte $enumerator_list) robust
							
							
							* Food consumption per capita: conso_food_pc  -- > NOT ROBUST
							cap drop interaction 
							gen interaction = trt_cash*married_b
							pdslasso conso_food_pc  trt_cash married_b interaction  ($balance $balance_hte $X_poly_and_int $enumerator_list ) , partial($balance $balance_hte $enumerator_list) robust
								
							
									
							************************************
							* conso at baseline (conso2_bl) 
							***************************************		
								
								
							
							* Used chemicals: chemicals  -- > NOT ROBUST
							cap drop interaction 
							gen interaction = trt_cash*conso2_bl
							pdslasso chemicals trt_cash conso2_bl interaction  ($balance $balance_hte $X_poly_and_int $enumerator_list ) , partial($balance $balance_hte $enumerator_list) robust
							
							* Value of the production: c1_cropvalna  -- > NOT ROBUST
							cap drop interaction 
							gen interaction = trt_cash*conso2_bl
							pdslasso c1_cropvalna trt_cash conso2_bl interaction  ($balance $balance_hte $X_poly_and_int $enumerator_list ) , partial($balance $balance_hte $enumerator_list) robust
							
							* Women financial index: z_financial_index
							cap drop interaction 
							gen interaction = trt_cash*conso2_bl
							pdslasso z_financial_index trt_cash conso2_bl interaction  ($balance $balance_hte $X_poly_and_int $enumerator_list ) , partial($balance $balance_hte $enumerator_list) robust
								
								
							* Livestock Bought since January 2019 (in Dirhams): value19_n_iga -- > NOT ROBUST
							cap drop interaction 
							gen interaction = trt_cash*conso2_bl
							pdslasso value19_n_iga trt_cash conso2_bl interaction  ($balance $balance_hte $X_poly_and_int $enumerator_list ) , partial($balance $balance_hte $enumerator_list) robust
							
							
							
							************************************
							* tclp beneficiaries (tclp) 
							***************************************						
							
							
							* Agriculture, quantity produced: c1_cropvalna -- > NOT ROBUST
							cap drop interaction 
							gen interaction = trt_cash*tclp
							pdslasso  c1_cropvalna trt_cash tclp interaction  ($balance $balance_hte $X_poly_and_int $enumerator_list ) , partial($balance $balance_hte $enumerator_list) robust
							






		
* TABLES S8-S10
* ----------------		
		
		use "${data}/data_women.dta" , clear
		do "${tables}/supplementary/stata-tex.do" 
		drop if attrition==1
		
			** Predicted outcome using RSS endogenous stratification
			** Predict each of our five outcomes using the repeated split sample (RSS) procedure developed by Abadie et al. (2018). 
			** We use 100 repetitions and all baseline controls as predictors (including outcomes). 
			set seed 123456789	
			* enumfe
			levelsof a1_enumerator, local(levels) 
			foreach l of local levels {
			gen enum_`l' = 1 if a1_enumerator == `l'
			replace enum_`l' = 0 if enum_`l' == .
			}
			global enumfe enum_2 enum_3 enum_5 enum_6 enum_7 enum_8 enum_9 enum_10 enum_11 enum_12 enum_13 enum_14 enum_15 enum_16 enum_17 enum_18 enum_19 enum_20 enum_21 enum_24 enum_25 enum_27 enum_28 enum_29
			* estimates
			foreach var in c1_job_iga iga_precovid business_profit business_employee total_employement c1_headjob c1_othermembersworkn c1_headincome30 c1_incomeotheriga /// 
						c1_hengag_agr chemicals c1_cropprodna c1_cropvalna does_livestock value19_n_iga value_n_iga /// 
						z_financial_index z_agency_index d1_ladder_present d1_ladder_1ago d1_ladder3years d1_ladderwealth mhi5 /// 
						conso_tot_pc conso_food_pc conso_nofood_pc z_assets_total z_assets19_total migration_d migration_future_d b4_econshock b4_othershock b4_extreme b4_normal  {
			reg `var' trt_cash $balance $baseline_outcomes $enumfe, robust
			local degreesf = e(df_r)
			estrat2 `var' trt_cash $balance $baseline_outcomes  , groups(3) reps(100) boot(100) rss_only cov($enumfe)
			matrix coefb=e(RSS_C)
			matrix se = e(RSS_SE)
			matrix colnames se = c1 c2 c3
			matrix VV1 = se'*se	
			mat b=e(RSS_C)
			mat c=e(RSS_SE)
			forvalues x=1/3 {
			local coef=(coefb[1,`x']) 
			local se=(se[1,`x']) 
			local text : di %6.3f `coef'
			local pvalue = (2 * ttail(`degreesf', abs(`coef'/`se')))
			count_stars, p(`pvalue')
			insert_into_file using sample_table.csv, key(`var'_rss`x'_starbeta) value(" `text'`r(stars)'") format(%6.3f)
			insert_into_file using sample_table.csv, key(`var'_rss`x'_se) value(" `se'") format(%6.3f)
			}
			local ttest12_t = (coefb[1,2] - coefb[1,1]) /(sqrt((se[1,2])^2+(se[1,1])^2))
			local ttest12_p = 2*ttail( `degreesf' ,abs(`ttest12_t'))
			local ttest13_t = (coefb[1,3] - coefb[1,1]) /(sqrt((se[1,3])^2+(se[1,1])^2))
			local ttest13_p = 2*ttail( `degreesf' ,abs(`ttest13_t'))
			local ttest23_t = (coefb[1,3] - coefb[1,2]) /(sqrt((se[1,3])^2+(se[1,2])^2))
			local ttest23_p = 2*ttail( `degreesf' ,abs(`ttest23_t'))
			insert_into_file using sample_table.csv, key(`var'_rss_p12) value(" `ttest12_p'") format(%6.3f)
			insert_into_file using sample_table.csv, key(`var'_rss_p13) value(" `ttest13_p'") format(%6.3f)
			insert_into_file using sample_table.csv, key(`var'_rss_p23) value(" `ttest23_p'") format(%6.3f)
			}

		
	table_from_tpl, t("TPL_tabS8.tex") r("sample_table.csv") o("tabS8.tex")
	table_from_tpl, t("TPL_tabS9.tex") r("sample_table.csv") o("tabS9.tex")
	table_from_tpl, t("TPL_tabS10.tex") r("sample_table.csv") o("tabS10.tex")




* 
* ttest differential take-up
di sqrt(0.946*(0.05))
di sqrt(0.952*(0.05))
ttesti 498 0.946 0.217 502 0.952 0.218

* MDES T1
foreach var in c1_job_iga conso_tot_pc z_assets_total {
sum `var' if trt_cash_2 == 0
pdslasso `var' trt_cash_2 ($balance $X_poly_and_int i.a1_enumerator tclp) , partial(i.a1_enumerator tclp) robust
}
di (0.018*2.8)/0.31
di (0.382*2.8)/7.21
di (0.055*2.8)/1.20

